/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System.Collections.Generic;

namespace Xceed.Silverlight.ListBox
{
  public class ListBoxGroupHeaderConfiguration : ListBoxContainerConfiguration<ListBoxGroupHeader>
  {
    #region CONSTRUCTORS

    public ListBoxGroupHeaderConfiguration()
      : base()
    {
    }

    protected ListBoxGroupHeaderConfiguration( ListBoxGroupHeaderConfiguration template )
      : base( template )
    {
      m_groupValueDataTemplates = template.GroupValueDataTemplates;
    }

    #endregion CONSTRUCTORS

    #region CanDrag Property

    internal override sealed bool CanDrag
    {
      get
      {
        return false;
      }
    }

    #endregion CanDrag Property

    #region CanStick Property

    public override bool CanStick
    {
      get
      {
        return true;
      }
    }

    #endregion CanStick Property

    #region GroupValueDataTemplates
    public List<GroupValueDataTemplate> GroupValueDataTemplates
    {
      get
      {
        return m_groupValueDataTemplates;
      }
    }
    #endregion

    #region PROTECTED METHODS

    protected override ListBoxGroupHeader CreateContainerCore()
    {
      return new ListBoxGroupHeader();
    }

    protected internal override ListBoxContainerConfigurationBase CreateInstance()
    {
      return new ListBoxGroupHeaderConfiguration( this );
    }

    protected override void PrepareContainerForItemCore( ListBoxGroupHeader container, object item )
    {
      base.PrepareContainerForItemCore( container, item );

      // Sets the GroupValueDataTemplate on the container so the HierarchicalGroupValueControl
      // can use them to with the ContentTemplate selector. 
      List<GroupValueDataTemplate> groupValueDataTemplates = this.GroupValueDataTemplates;

      if( groupValueDataTemplates != null )
      {
        container.GroupValueDataTemplates = groupValueDataTemplates;
      }
    }

    protected override void EndInitCore()
    {
      base.EndInitCore();

      // Validate GroupValueDataTemplates
      // Make sure a field name is only associted with one DataTemplate.
      List<string> fieldNames = new List<string>();

      foreach( GroupValueDataTemplate groupValueDataTemplate in this.GroupValueDataTemplates )
      {
        foreach( string fieldName in groupValueDataTemplate.FieldNames )
        {
          if( fieldNames.Contains( fieldName ) )
          {
            throw new ListBoxException( "The field name is already associated with a DataTemplate." );
          }
          fieldNames.Add( fieldName );
        }
      }
    }

    #endregion PROTECTED METHODS

    #region INTERNAL METHODS

    internal override ConfigurationType GetConfigurationType()
    {
      return ConfigurationType.GroupHeader;
    }

    #endregion

    #region Private Members
    List<GroupValueDataTemplate> m_groupValueDataTemplates = new List<GroupValueDataTemplate>();
    #endregion
  }
}
